import re
from time import ctime


# 17 统计周一到周日出现的次数
days = {'Mon': 0, 'Tue': 0, 'Wed': 0, 'Thu': 0, 'Fri': 0, 'Sat': 0, 'Sun': 0}
with open('redata.txt', 'r') as f:
    r = f.readlines()
    for i in r:
        result = re.match(r'^(\w{3})', i).group(0)
        days[result] += 1

# 18 匹配第一个整数字段
with open('redata.txt', 'r') as f:
    r = f.readlines()
    for i in r:
        re_int = int(re.search(r'(\d+)-\d+-\d+', i).group(1))
        re_time = re.search(r'(.+?)::', i).group(1)
        if ctime(re_int) != re_time:
            print(re_int, '!=', re_time)

# 20 提取完整的邮箱地址
with open('redata.txt', 'r') as f:
    r = f.readlines()
    for i in r:
        result = re.search(r'.+::(.+)::.+',i).group(1)


# 21 提取时间戳中的月份
with open('redata.txt', 'r') as f:
    r = f.readlines()
    for i in r:
        result = re.match(r'^\w{3}\s(\w{3})', i).group(1)


# 22 提取时间戳中的年份
with open('redata.txt', 'r') as f:
    r = f.readlines()
    for i in r:
        result = re.match(r'^\w{3}\s\w{3}\s+\d+\s.+?\s(\d+)::', i).group(1)

# 23 提取时间戳中的时间
with open('redata.txt', 'r') as f:
    r = f.readlines()
    for i in r:
        result = re.match(r'^\w{3}\s\w{3}\s+\d+\s(.+?)\s\d+::', i).group(1)

# 27 将时间整理为月、日、年的格式
with open('redata.txt', 'r') as f:
    r = f.readlines()
    # 多次运用某个正则，先编译再匹配效率更高
    result = re.compile(r'^\w{3}\s(?P<month>\w{3})\s+(?P<day>\d+)\s.+?\s(?P<year>\d+)::')
    for i in r:
        result_ = result.match(i)
        print(result_.groupdict())
